package itsm.isperp.job;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.LinkedList;
import java.util.logging.Logger;

import javax.sql.DataSource;

public class SDDataSourcePool implements DataSource {

	private static String url = null;

	private static String username = null;

	private static String password = null;

	private static int size = 10;

	private static LinkedList<Connection> list = new LinkedList<Connection>();

	private static SDDataSourcePool pool = null;

	public static void init(String url_, String username_, String password_) {
		url = url_;
		username = username_;
		password = password_;
	}

	// 创建对象就初始化size个数据库连接

	private SDDataSourcePool() {

		for (int i = 0; i < 2; i++) {
			try {
				Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
						.newInstance();
				 
				Connection conn = DriverManager.getConnection(url, username,
						password);
				list.add(conn);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	public static SDDataSourcePool getInstance() {
		pool = new SDDataSourcePool();
		return pool;

	}

	@Override
	public Connection getConnection() throws SQLException {

		if (list.size() > 0) {
			// 取到连接，即从list中弹出一个Connection 连接
			final Connection conn = list.pop();
			return conn;
		}
		// 连接用完
		throw new RuntimeException("对不起，服务器繁忙！！！");
	}

	@Override
	public Connection getConnection(String username, String password)
			throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	}
}